import 'package:flutter_quick/Interfaces/IM.dart';
import 'package:flutter_quick/common/utils/GenerateTestUserSig.dart';
import 'package:tencent_im_sdk_plugin/enum/V2TimSDKListener.dart';
import 'package:tencent_im_sdk_plugin/enum/log_level_enum.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_conversation_result.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_message.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_user_full_info.dart';
import 'package:tencent_im_sdk_plugin/tencent_im_sdk_plugin.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_value_callback.dart';
import 'package:tencent_im_sdk_plugin/manager/v2_tim_manager.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_callback.dart';

class TIM implements IMInterface {
  V2TIMManager timManager = TencentImSDKPlugin.v2TIMManager;

  TIM() {}

  @override
  init() async {
    //初始化SDK
    V2TimValueCallback<bool> initRes = await timManager.initSDK(
      sdkAppID: 1400551505, //填入在控制台上申请的sdkappid
      loglevel: LogLevelEnum.V2TIM_LOG_NONE,
      listener: V2TimSDKListener(
        onConnecting: () => {},
        onConnectSuccess: () => {},
        onConnectFailed: (code, error) => {},
        onKickedOffline: () => {},
        onUserSigExpired: () => {},
        onSelfInfoUpdated: (V2TimUserFullInfo info) => {},
      ),
    );

    if (initRes.code == 0) {
      print('初始化成功');
    } else {
      throw false;
    }
  }

  @override
  login() async {
    String userID = 'u_1';
    // 正式环境请在服务端计算userSIg
    String userSig = GenerateTestUserSig(
      sdkappid: 1400551505,
      key: '0cda1401501ffadcfcf242bbebd354d3316b6040b59861fd245e859466d52a8e',
    ).genSig(
      identifier: userID,
      expire: 7 * 24 * 60 * 1000, // userSIg有效期
    );

    V2TimCallback loginRes =
        await timManager.login(userID: userID, userSig: userSig);
    //V2TimCallback 返回的所有数据结构
    int code = loginRes.code;
    String desc = loginRes.desc;

    if (code == 0) {
      // 登录成功
      print('登录成功');
    } else {
      // 登录失败
      throw false;
    }
  }

  @override
  acceptGroupApplication() {
    // TODO: implement acceptGroupApplication
    throw UnimplementedError();
  }

  @override
  addToBlackList() {
    // TODO: implement addToBlackList
    throw UnimplementedError();
  }

  @override
  createGroup() {
    // TODO: implement createGroup
    throw UnimplementedError();
  }

  @override
  deleteConversation() {
    // TODO: implement deleteConversation
    throw UnimplementedError();
  }

  @override
  deleteFromBlackList() {
    // TODO: implement deleteFromBlackList
    throw UnimplementedError();
  }

  @override
  deleteMessageFromLocalStorage() {
    // TODO: implement deleteMessageFromLocalStorage
    throw UnimplementedError();
  }

  @override
  dismissGroup() {
    // TODO: implement dismissGroup
    throw UnimplementedError();
  }

  @override
  getBlackList() {
    // TODO: implement getBlackList
    throw UnimplementedError();
  }

  @override
  Future<List<MessageItemItem>> getC2CHistoryMessageList() async {
    V2TimValueCallback<List<V2TimMessage>> v2TimMessage = await timManager
        .getMessageManager()
        .getC2CHistoryMessageList(userID: 'u_2', count: 20);

    return v2TimMessage.data?.map((e) {
          TextMessageContent? textMessageContent;

          switch (e.runtimeType) {
            case V2TimMessage:
              textMessageContent =
                  TextMessageContent(text: e.textElem?.text ?? '');
              break;
            default:
          }

          return MessageItemItem(
            avatar: e.faceUrl ?? '',
            name: e.nickName ?? '',
            textMessageContent: textMessageContent,
          );
        }).toList() ??
        [];
  }

  @override
  Future<List<ConversationItem>> getConversationList() async {
    V2TimValueCallback<V2TimConversationResult> v2timValueCallback =
        await timManager
            .getConversationManager()
            .getConversationList(nextSeq: '0', count: 10);
    return v2timValueCallback.data?.conversationList
            ?.map((e) => ConversationItem(
                  avatar: e?.faceUrl ?? '',
                  title: e?.showName ?? '',
                  type: ConversationItemType.apple,
                  lastMsg: e?.lastMessage?.textElem?.text ?? '',
                  uid: e?.userID ?? '',
                ))
            .toList() ??
        [];
  }

  @override
  getFriendList() async {
    V2TimValueCallback<List<V2TimFriendInfo>> timFriendInfo =
        await timManager.getFriendshipManager().getFriendList();
    print(timFriendInfo);
  }

  @override
  getGroupApplicationList() {
    // TODO: implement getGroupApplicationList
    throw UnimplementedError();
  }

  @override
  getGroupHistoryMessageList() {
    // TODO: implement getGroupHistoryMessageList
    throw UnimplementedError();
  }

  @override
  getGroupMemberList() {
    // TODO: implement getGroupMemberList
    throw UnimplementedError();
  }

  @override
  getGroupMembersInfo() {
    // TODO: implement getGroupMembersInfo
    throw UnimplementedError();
  }

  @override
  getGroupsInfo() {
    // TODO: implement getGroupsInfo
    throw UnimplementedError();
  }

  @override
  getJoinedGroupList() {
    // TODO: implement getJoinedGroupList
    throw UnimplementedError();
  }

  @override
  getLoginStatus() {
    // TODO: implement getLoginStatus
    throw UnimplementedError();
  }

  @override
  getLoginUser() {
    // TODO: implement getLoginUser
    throw UnimplementedError();
  }

  @override
  getUsersInfo() async {
    V2TimValueCallback<List<V2TimUserFullInfo>> v2TimUserFullInfo =
        await timManager.getUsersInfo(userIDList: ['u_1']);
    print(v2TimUserFullInfo.toJson());
  }

  @override
  insertGroupMessageToLocalStorage() {
    // TODO: implement insertGroupMessageToLocalStorage
    throw UnimplementedError();
  }

  @override
  inviteUserToGroup() {
    // TODO: implement inviteUserToGroup
    throw UnimplementedError();
  }

  @override
  joinGroup() {
    // TODO: implement joinGroup
    throw UnimplementedError();
  }

  @override
  kickGroupMember() {
    // TODO: implement kickGroupMember
    throw UnimplementedError();
  }

  @override
  logout() {
    // TODO: implement logout
    throw UnimplementedError();
  }

  @override
  markC2CMessageAsRead() {
    // TODO: implement markC2CMessageAsRead
    throw UnimplementedError();
  }

  @override
  markGroupMessageAsRead() {
    // TODO: implement markGroupMessageAsRead
    throw UnimplementedError();
  }

  @override
  muteGroupMember() {
    // TODO: implement muteGroupMember
    throw UnimplementedError();
  }

  @override
  quitGroup() {
    // TODO: implement quitGroup
    throw UnimplementedError();
  }

  @override
  refuseGroupApplication() {
    // TODO: implement refuseGroupApplication
    throw UnimplementedError();
  }

  @override
  sendCustomMessage() {}

  @override
  sendFileMessage() {
    // TODO: implement sendFileMessage
    throw UnimplementedError();
  }

  @override
  sendImageMessage() {
    // TODO: implement sendImageMessage
    throw UnimplementedError();
  }

  @override
  sendLocationMessage() {
    // TODO: implement sendLocationMessage
    throw UnimplementedError();
  }

  @override
  sendSoundMessage() {
    // TODO: implement sendSoundMessage
    throw UnimplementedError();
  }

  @override
  sendVideoMessage() {
    // TODO: implement sendVideoMessage
    throw UnimplementedError();
  }

  @override
  setConversationDraft() {
    // TODO: implement setConversationDraft
    throw UnimplementedError();
  }

  @override
  setConversationListener() {
    // TODO: implement setConversationListener
    throw UnimplementedError();
  }

  @override
  setGroupApplicationRead() {
    // TODO: implement setGroupApplicationRead
    throw UnimplementedError();
  }

  @override
  setGroupInfo() {
    // TODO: implement setGroupInfo
    throw UnimplementedError();
  }

  @override
  setGroupMemberInfo() {
    // TODO: implement setGroupMemberInfo
    throw UnimplementedError();
  }

  @override
  setGroupMemberRole() {
    // TODO: implement setGroupMemberRole
    throw UnimplementedError();
  }

  @override
  setReceiveMessageOpt() {
    // TODO: implement setReceiveMessageOpt
    throw UnimplementedError();
  }

  @override
  setSelfInfo() async {
    V2TimCallback v2timCallback = await timManager.setSelfInfo(
        userFullInfo: V2TimUserFullInfo(
            nickName: "张三",
            faceUrl:
                'https://images.pexels.com/photos/8783490/pexels-photo-8783490.jpeg?auto=compress&cs=tinysrgb&dpr=1&h=140&w=140'));
    print(v2timCallback.toJson());
  }

  @override
  transferGroupOwner() {
    // TODO: implement transferGroupOwner
    throw UnimplementedError();
  }

  @override
  unInit() {
    // TODO: implement unInitSDK
    throw UnimplementedError();
  }

  @override
  sendTextMessage() async {
    V2TimValueCallback<V2TimMessage> v2TimMessage =
        await timManager.sendC2CTextMessage(text: 'test', userID: 'u_2');
  }

  @override
  sendGroupTextMessage() {
    // TODO: implement sendGroupTextMessage
    throw UnimplementedError();
  }
}
